<template>
  <div class="login">
    <el-form
      :model="ruleForm2"
      :rules="rules2"
      ref="ruleForm2"
      label-position="left"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">万绿城集团采购系统</h3>
      <el-form-item prop="account">
        <el-input
          @keyup.enter.native="handleSubmit2"
          type="text"
          v-model="ruleForm2.account"
          auto-complete="off"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password" v-if="!forget">
        <el-input
          @keyup.enter.native="handleSubmit2"
          type="password"
          v-model="ruleForm2.password"
          auto-complete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="verificationCode" v-if="!forget">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-input
              type="text"
              v-model="ruleForm2.verificationCode"
              auto-complete="off"
              placeholder="验证码"
              @keyup.enter.native="handleSubmit2"
            ></el-input>
          </el-col>
          <el-col :span="8">
            <div class="codeImg" @click="getCodeImg">
              <img :src="codeUrl" alt />
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <div class="form-line">
        <div>
          <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
        </div>
        <div>
          <el-link type="primary" @click="forgetPassword()">{{forget?'重新登录':'忘记密码'}}</el-link>
        </div>
      </div>

      <el-form-item style="width:100%;">
        <el-button
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleSubmit2"
          :loading="logining"
        >{{forget?'申请忘记密码':'登录'}}</el-button>
        <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { requestLogin } from "../api/api";
import { Login, forgetPassword } from "../api/login";
import API from "../api/webpackApi";
//import NProgress from 'nprogress'
export default {
  data() {
    return {
      logining: false,
      ruleForm2: {
        // account: "admin",
        // password: "111111",
        account: "",
        password: "",
        verificationCode: ""
      },
      rules2: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" }
          //{ validator: validaePass }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
          //{ validator: validaePass2 }
        ],
        verificationCode: [
          { required: true, message: "请输入验证码", trigger: "blur" }
        ]
      },
      checked: true,
      codeUrl: "",
      forget: false
    };
  },
  methods: {
    // 点击忘记密码
    forgetPassword() {
      this.forget = !this.forget;
      this.handleReset2();
    },
    handleReset2() {
      this.$refs.ruleForm2.resetFields();
    },
    //走忘记密码接口
    postForget() {
      forgetPassword({
        account: this.ruleForm2.account
      }).then(res => {
        this.logining = false;
        this.$message({
          message: "申请提交成功，请耐心等待",
          type: "success"
        });
      });
    },
    handleSubmit2(ev) {
      var _this = this;
      this.$refs.ruleForm2.validate(valid => {
        if (valid) {
          this.logining = true;
          if (this.forget) {
            this.postForget();
          } else {
            Login(this.ruleForm2)
              .then(res => {
                this.logining = false;
                if (res) {
                  this.$store.commit("setToken", res.token);
                  sessionStorage.setItem("menus", JSON.stringify(res.menus));
                  sessionStorage.setItem('userName',res.user.userName);
                  if (this.checked) {
                    localStorage.setItem("token", res.token);
                  }
                  this.$router.replace({ path: "/main" });
                }
              })
              .catch(err => {
                this.logining = false;
                localStorage.setItem("token", "");
                this.$router.replace({ path: "/main" });
              });
          }
        }
      });
    },

    //刷新验证码
    getCodeImg() {
      var timestamp = Date.parse(new Date());
      this.codeUrl = API.url + "verificationCode?t=" + timestamp;
    }
  },
  created() {
    this.getCodeImg();
  }
};
</script>

<style lang="scss" scoped>
.login {
  background: url(../assets/loginBG.png) no-repeat;
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  .form-line {
    display: flex;
    justify-content: space-between;
  }
}
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
  .codeImg {
    height: 40px;
    display: inline-block;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>